//
//  ZJPublicFormTableViewCell.m
//  ZJControlsClass
//
//  Created by apple on 2024/10/30.
//

#import "ZJPublicFormTableViewCell.h"


@interface ZJPublicFormTableViewCell ()<UITextFieldDelegate>



@property (nonatomic,strong) UIView * v;





@end




@implementation ZJPublicFormTableViewCell


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        
        [self configUI];
        
        
        
    }
    return self;
}


-(void)configUI{
    
    [self.contentView addSubview:self.requiredLabel];
    [self.contentView addSubview:self.titleLabel];
    [self.contentView addSubview:self.inputTextF];
    [self.contentView addSubview:self.rightImg];
    [self.contentView addSubview:self.v];
    
    
    [self.requiredLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.contentView.mas_centerY);
        make.left.equalTo(self.contentView).with.offset(ALIGN_WIDTH_PT(15));
        make.size.equalTo(@(CGSizeMake(10, 10)));
    }];
    
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.contentView.mas_centerY);
        make.left.equalTo(self.requiredLabel.mas_right);
        make.height.equalTo(@(ALIGN_HEIGHT_PT(50)));
        make.width.equalTo(@(SCREEN_WIDTH / 2 - ALIGN_WIDTH_PT(15)));
    }];
    
    [self.inputTextF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.titleLabel.mas_centerY);
        make.left.equalTo(self.titleLabel.mas_right);
        make.right.equalTo(self.contentView).with.offset(-ALIGN_WIDTH_PT(30));
        make.width.equalTo(@(SCREEN_WIDTH - ALIGN_WIDTH_PT(30) / 2));
        make.height.equalTo(@(ALIGN_HEIGHT_PT(50)));
    }];
    
    [self.rightImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.titleLabel.mas_centerY);
        make.right.equalTo(self.contentView).with.offset(-ALIGN_WIDTH_PT(10));
        make.width.equalTo(@(ALIGN_WIDTH_PT(6)));
        make.height.equalTo(@(ALIGN_WIDTH_PT(10)));
    }];
    
    [self.v mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).with.offset(ALIGN_WIDTH_PT(15));
        make.right.equalTo(self.contentView).with.offset(-ALIGN_WIDTH_PT(15));
        make.bottom.equalTo(self.contentView.mas_bottom).with.offset(-ALIGN_HEIGHT_PT(0.5));
        make.height.equalTo(@(ALIGN_HEIGHT_PT(0.5)));
    }];
    
}

-(void)setModel:(HomeUnitModel *)model{
    _model = model;
    
    if ([model.props.required boolValue] == YES) {
        self.requiredLabel.hidden = NO;
    }else{
        self.requiredLabel.hidden = YES;
    }
    if (self.model.props.input.length != 0) {
        self.inputTextF.text = self.model.props.input;
    }else{
        self.inputTextF.placeholder = self.model.props.placeholder;
    }

//    if ([model.props.title isEqualToString:@""]){
//
//        self.titleLabel.text = model.props.title;
//    }else{
        self.titleLabel.text = self.model.props.title;
//    }
    
       if ([model.title isEqualToString:@"小标题"]) {
           self.titleLabel.font = [UIFont systemFontOfSize:16 weight:0.3];
           self.rightImg.hidden = YES;
           self.inputTextF.enabled = NO;
           self.v.hidden = YES;

       }else{
           self.titleLabel.font = FONT14;
           self.rightImg.hidden = NO;
           self.inputTextF.enabled = YES;
           self.v.hidden = NO;
           
           if ([model.valueType isEqualToString:@"String"] || [model.valueType isEqualToString:@"Textarea"]) {
               self.rightImg.hidden = YES;
               self.inputTextF.enabled = YES;
           }else{
               self.rightImg.hidden = NO;
               self.inputTextF.enabled = NO;
           }


       }
    if ([model.name isEqualToString:@"currentOrgName"]){
        self.inputTextF.text = self.model.props.input;
        self.inputTextF.enabled = NO;
    }
}


- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    NSString *newText = [textField.text stringByReplacingCharactersInRange:range withString:string];
    
    // 如果有最大长度限制
    if (self.model.props.maxlength && [self.model.props.maxlength integerValue] > 0) {
        if (newText.length > [self.model.props.maxlength integerValue]) {
            return NO;
        }
    }
    
    return YES;
}



-(void)textFieldDidEndEditing:(UITextField *)textField{
    self.model.props.input = textField.text;
}

#pragma mark -- 懒加载

-(UILabel *)requiredLabel{
    if (!_requiredLabel) {
        _requiredLabel = [UILabel new];
        _requiredLabel.text = @"*";
        _requiredLabel.textColor = [UIColor redColor];
        _requiredLabel.font = FONT14;
        _requiredLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _requiredLabel;
}

-(UILabel *)titleLabel{
    if (!_titleLabel) {
        _titleLabel = [UILabel new];
        _titleLabel.text = @"申办单位全称";
        _titleLabel.font = FONT14;
        _titleLabel.textColor = [UIColor blackColor];
        _titleLabel.textAlignment = NSTextAlignmentLeft;
    }
    return _titleLabel;
}

-(UITextField *)inputTextF{
    if (!_inputTextF) {
        _inputTextF = [UITextField new];
        _inputTextF.textColor = [UIColor blackColor];
        _inputTextF.font = FONT14;
        _inputTextF.textAlignment = NSTextAlignmentLeft;
        _inputTextF.delegate = self;
    }
    return _inputTextF;
}

-(UIImageView *)rightImg{
    if (!_rightImg) {
        _rightImg = [UIImageView new];
        _rightImg.image = [UIImage imageNamed:@"Vector"];
    }
    return _rightImg;
}
-(UIView *)v{
    if (!_v) {
        _v = [UIView new];
        _v.backgroundColor = COLOR_RGB(215, 216, 217);
    }
    return _v;
}




@end
